<template>
  <div class="users">
    <el-card class="search-card">
      <el-form :model="searchForm" label-width="80px" inline>
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="启用" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <el-card class="table-card">
      <template #header>
        <div class="card-header">
          <span>用户列表</span>
        </div>
      </template>
      
      <el-table
        :data="userList"
        style="width: 100%"
        v-loading="loading"
        border
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="nickname" label="昵称" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="lastLoginTime" label="最后登录" width="180" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button 
              size="small" 
              :type="scope.row.status === 1 ? 'danger' : 'success'"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 1 ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 编辑用户对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="编辑用户"
      width="500px"
    >
      <el-form
        ref="userFormRef"
        :model="userForm"
        :rules="userRules"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username" disabled />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="userForm.nickname" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userForm.email" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="userForm.phone" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="userForm.status"
            :active-value="1"
            :inactive-value="0"
            active-text="启用"
            inactive-text="禁用"
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmUser">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'Users',
  setup() {
    const loading = ref(false)
    const dialogVisible = ref(false)
    const userFormRef = ref(null)
    
    // 搜索表单
    const searchForm = reactive({
      username: '',
      status: ''
    })
    
    // 分页
    const pagination = reactive({
      currentPage: 1,
      pageSize: 10,
      total: 0
    })
    
    // 用户表单
    const userForm = reactive({
      id: null,
      username: '',
      nickname: '',
      email: '',
      phone: '',
      status: 1
    })
    
    // 表单验证规则
    const userRules = {
      nickname: [
        { required: true, message: '请输入昵称', trigger: 'blur' }
      ],
      email: [
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ]
    }
    
    // 模拟用户数据
    const userList = ref([
      {
        id: 1,
        username: 'admin',
        nickname: '管理员',
        email: 'admin@example.com',
        phone: '13800138000',
        avatar: '',
        createTime: '2023-05-01 10:00:00',
        lastLoginTime: '2023-05-15 14:30:00',
        status: 1
      },
      {
        id: 2,
        username: 'user1',
        nickname: '用户1',
        email: 'user1@example.com',
        phone: '13800138001',
        avatar: '',
        createTime: '2023-05-02 10:00:00',
        lastLoginTime: '2023-05-14 09:15:00',
        status: 1
      },
      {
        id: 3,
        username: 'user2',
        nickname: '用户2',
        email: 'user2@example.com',
        phone: '13800138002',
        avatar: '',
        createTime: '2023-05-03 10:00:00',
        lastLoginTime: '2023-05-10 16:45:00',
        status: 0
      }
    ])
    
    // 设置分页总数
    pagination.total = userList.value.length
    
    const handleSearch = () => {
      ElMessage.info('执行搜索')
    }
    
    const resetSearch = () => {
      searchForm.username = ''
      searchForm.status = ''
      ElMessage.info('重置搜索条件')
    }
    
    const handleEdit = (row) => {
      // 填充表单数据
      Object.keys(userForm).forEach(key => {
        userForm[key] = row[key]
      })
      dialogVisible.value = true
    }
    
    const handleToggleStatus = (row) => {
      const action = row.status === 1 ? '禁用' : '启用'
      ElMessageBox.confirm(
        `确定要${action}用户《${row.username}》吗？`,
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        // 切换状态
        row.status = row.status === 1 ? 0 : 1
        ElMessage.success(`${action}成功`)
      }).catch(() => {
        ElMessage.info('已取消操作')
      })
    }
    
    const confirmUser = () => {
      userFormRef.value.validate((valid) => {
        if (valid) {
          // 更新用户信息
          const index = userList.value.findIndex(item => item.id === userForm.id)
          if (index !== -1) {
            Object.keys(userForm).forEach(key => {
              userList.value[index][key] = userForm[key]
            })
            ElMessage.success('更新成功')
          }
          
          dialogVisible.value = false
        } else {
          ElMessage.error('请填写正确的信息')
        }
      })
    }
    
    const handleSizeChange = (val) => {
      pagination.pageSize = val
      ElMessage.info(`每页 ${val} 条`)
    }
    
    const handleCurrentChange = (val) => {
      pagination.currentPage = val
      ElMessage.info(`当前页: ${val}`)
    }
    
    return {
      loading,
      dialogVisible,
      userFormRef,
      searchForm,
      pagination,
      userForm,
      userRules,
      userList,
      handleSearch,
      resetSearch,
      handleEdit,
      handleToggleStatus,
      confirmUser,
      handleSizeChange,
      handleCurrentChange
    }
  }
}
</script>

<style scoped>
.search-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>